﻿@using eLoan.Models;
@model UserModel

@{
    ViewBag.Title = "Add/Edit User";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Add/Edit User</h2>
        <ol class="breadcrumb">
            <li>
                Administration
            </li>
            <li>
                <a href="@Url.Action("Users", "Administration")">Users</a>
            </li>
            <li class="active">
                <strong>Add/Edit User</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>
@using (Html.BeginForm("Save", "User", FormMethod.Post, new { id = "saveForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
    <fieldset>
        <div class="wrapper wrapper-content animated fadeInRight">
            <div class="row">
                <div class="col-lg-12">
                    <div class="ibox float-e-margins" >
                        <div id="divValidations" class="ibox-title">
                            <div>@Html.ValidationMessageFor(m => m.User.UserName)</div>
                            <div>@Html.ValidationMessageFor(m => m.Password)</div>
                            <div>@Html.ValidationMessageFor(m => m.User.BranchCode)</div>
                            <div>@Html.ValidationMessageFor(m => m.User.RoleID)</div>
                        </div>
                        <div class="ibox-content">
                            <table class="table table-bordered table-striped" style="width:50%">
                                <tr>
                                    <th>
                                        @if (Model.DBCommand == "Edit")
                                        { @Html.HiddenFor(m => m.User.ID) }
                                        @Html.LabelFor(m => m.User.UserName)
                                    </th>
                                    <td>
                                        @if (Model.DBCommand == "Add")
                                        {
                                            @Html.TextBoxFor(m => m.User.UserName, new { @id = "txtUserName", @class = "text-box single-line scroll-field1" })
                                        }
                                        else
                                        {
                                            @Html.HiddenFor(m => m.User.UserName)
                                            @Html.DisplayFor(m => m.User.UserName)
                                        }

                                    </td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.Password)</th>
                                    <td>
                                        @if (Model.DBCommand == "Add")
                                        {
                                            @Html.PasswordFor(m => m.Password, new { @id = "txtPassword", @class = "text-box single-line scroll-field1" })
                                        }
                                        else
                                        {
                                            <span>**********</span>
                                            @Html.HiddenFor(m => m.Password)
                                        }
                                    </td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.User.BranchCode)</th>
                                    <td>@Html.DropDownListFor(m => m.User.BranchCode, Model.Branches, new { @class = "text-box single-line scroll-field1" })</td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.User.RoleID)</th>
                                    <td>@Html.DropDownListFor(m => m.User.RoleID, Model.Roles, new { @id = "ddlRole", @class = "text-box single-line scroll-field1", @onclick = "CheckAccess()" })</td>
                                </tr>
                                <tr>
                                    <th>@Html.LabelFor(m => m.User.IsActive)</th>
                                    <td>@Html.CheckBoxFor(m => m.User.IsActive)</td>
                                </tr>
                            </table>
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th class="k-grid-header">
                                            <ul class="checkbox">
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasAdministrationAccess, new { @class = "chkAllAdmin", @onclick = "chkAll(this, '.chkAdmin')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasAdministrationAccess)
                                                </li>
                                            </ul>
                                        </th>
                                        <th class="k-grid-header">
                                            <ul class="checkbox">
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasMasterFilesAccess, new { @class = "chkAllMaster", @onclick = "chkAll(this, '.chkMaster')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasMasterFilesAccess)
                                                </li>
                                            </ul>
                                        </th>
                                        <th class="k-grid-header">
                                            <ul class="checkbox">
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasFundingsAccess, new { @class = "chkAllFundings", @onclick = "chkAll(this, '.chkFundings')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasFundingsAccess)
                                                </li>
                                            </ul>
                                        </th>
                                        <th class="k-grid-header">
                                            <ul class="checkbox">
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasWorkspaceAccess, new { @class = "chkAllWorkspace", @onclick = "chkAll(this, '.chkWorkspace')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasWorkspaceAccess)
                                                </li>
                                            </ul>
                                        </th>
                                        <th class="k-grid-header">
                                            <ul class="checkbox">
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasReportsAccess, new { @class = "chkAllReports", @onclick = "chkAll(this, '.chkReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasReportsAccess)
                                                </li>
                                            </ul>
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>
                                            <ul class="checkbox">
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasUsersAccess, new { @class = "chkAdmin", @onclick = "checkMe('.chkAdmin', '.chkAllAdmin')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasUsersAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasBanksAccess, new { @class = "chkAdmin", @onclick = "checkMe('.chkAdmin', '.chkAllAdmin')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasBanksAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasBranchesAccess, new { @class = "chkAdmin", @onclick = "checkMe('.chkAdmin', '.chkAllAdmin')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasBranchesAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasAgentsAccess, new { @class = "chkAdmin", @onclick = "checkMe('.chkAdmin', '.chkAllAdmin')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasAgentsAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasLoanTypesAccess, new { @class = "chkAdmin", @onclick = "checkMe('.chkAdmin', '.chkAllAdmin')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasLoanTypesAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasChargeTypesAccess, new { @class = "chkAdmin", @onclick = "checkMe('.chkAdmin', '.chkAllAdmin')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasChargeTypesAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasPaymentTypesAccess, new { @class = "chkAdmin", @onclick = "checkMe('.chkAdmin', '.chkAllAdmin')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasPaymentTypesAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasNotificationsAccess, new { @class = "chkAdmin", @onclick = "checkMe('.chkAdmin', '.chkAllAdmin')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasNotificationsAccess)
                                                </li>
                                            </ul>
                                        </td>
                                        <td>
                                            <ul class="checkbox">
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasAddressBookAccess, new { @class = "chkMaster", @onclick = "checkMe('.chkMaster', '.chkAllMaster')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasAddressBookAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasProspectsAccess, new { @class = "chkMaster", @onclick = "checkMe('.chkMaster', '.chkAllMaster')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasProspectsAccess)
                                                </li>
                                            </ul>
                                        </td>
                                        <td>
                                            <ul class="checkbox">
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasLoansAccess, new { @class = "chkFundings", @onclick = "checkMe('.chkFundings', '.chkAllFundings')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasLoansAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasCancelLoanAccess, new { @class = "chkFundings", @onclick = "checkMe('.chkFundings', '.chkAllFundings')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasCancelLoanAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasPrintChequeAccess, new { @class = "chkFundings", @onclick = "checkMe('.chkFundings', '.chkAllFundings')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasPrintChequeAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasPrintContractAccess, new { @class = "chkFundings", @onclick = "checkMe('.chkFundings', '.chkAllFundings')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasPrintContractAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasReleaseToBranchAccess, new { @class = "chkFundings", @onclick = "checkMe('.chkFundings', '.chkAllFundings')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasReleaseToBranchAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasReleaseToCustomerAccess, new { @class = "chkFundings", @onclick = "checkMe('.chkFundings', '.chkAllFundings')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasReleaseToCustomerAccess)
                                                </li>
                                            </ul>
                                        </td>
                                        <td>
                                            <ul class="checkbox">
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasLoanPaymentAccess, new { @class = "chkWorkspace", @onclick = "checkMe('.chkWorkspace', '.chkAllWorkspace')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasLoanPaymentAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasChequeClearingAccess, new { @class = "chkWorkspace", @onclick = "checkMe('.chkWorkspace', '.chkAllWorkspace')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasChequeClearingAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasLoanPenaltyAccess, new { @class = "chkWorkspace", @onclick = "checkMe('.chkWorkspace', '.chkAllWorkspace')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasLoanPenaltyAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasVoidLoanPaymentAccess, new { @class = "chkWorkspace", @onclick = "checkMe('.chkWorkspace', '.chkAllWorkspace')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasVoidLoanPaymentAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasRequestCloseLoanAccess, new { @class = "chkWorkspace", @onclick = "checkMe('.chkWorkspace', '.chkAllWorkspace')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasRequestCloseLoanAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasCloseLoanAccess, new { @class = "chkWorkspace", @onclick = "checkMe('.chkWorkspace', '.chkAllWorkspace')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasCloseLoanAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasLoanNotificationAccess, new { @class = "chkWorkspace", @onclick = "checkMe('.chkWorkspace', '.chkAllWorkspace')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasLoanNotificationAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasDailyCollectionReconciliationAccess, new { @class = "chkWorkspace", @onclick = "checkMe('.chkWorkspace', '.chkAllWorkspace')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasDailyCollectionReconciliationAccess)
                                                </li>
                                            </ul>
                                        </td>
                                        <td>
                                            <ul class="checkbox">
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasAgentItineraryAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasAgentItineraryAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasCollectionReportAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasCollectionReportAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasSalesReportAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasSalesReportAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasChequeReportAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasChequeReportAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasSavingsReportAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasSavingsReportAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasSOAAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasSOAAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasPenaltyHistoryAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasPenaltyHistoryAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasClosedLoansReportAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasClosedLoansReportAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasDuePaymentsAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasDuePaymentsAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasDailyCollectionReconciliationReportAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasDailyCollectionReconciliationReportAccess)
                                                </li>
                                                <li>
                                                    @Html.CheckBoxFor(m => m.UserAccess.HasAgingReportAccess, new { @class = "chkReports", @onclick = "checkMe('.chkReports', '.chkAllReports')" })
                                                    @Html.LabelFor(m => m.UserAccess.HasAgingReportAccess)
                                                </li>
                                            </ul>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            <p></p>
                            <button type="submit" id="btnSave" class="btn btn-primary" name="command" value=@Model.DBCommand>Save</button>
                            <button type="submit" id="btnCancel" class="btn btn-primary cancel" name="command" value="Cancel">Cancel</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
    </fieldset>
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">

        function chkAll(me, chkClass)
        {
            var val = $(me).is(':checked');

            $(chkClass).each(function () {
                $(this).prop("checked", val);
            });
        }

        function checkMe(chkMe, chkAll) {
            var allIsChecked = true;

            $(chkMe).each(function () {
                val = $(this).is(':checked');

                if (!val)
                    allIsChecked = false;
            });

            $(chkAll).prop('checked', allIsChecked);
        }

        function CheckAccess()
        {
            var val = $('#ddlRole').val();

            if (val == 1) {
                $('.chkAllAdmin').prop('checked', true);
                $('.chkAdmin').prop('checked', true);

                $('.chkAllMaster').prop('checked', true);
                $('.chkMaster').prop('checked', true);

                $('.chkAllFundings').prop('checked', true);
                $('.chkFundings').prop('checked', true);

                $('.chkAllWorkspace').prop('checked', true);
                $('.chkWorkspace').prop('checked', true);

                $('.chkAllReports').prop('checked', true);
                $('.chkReports').prop('checked', true);
            }
            else {
                $('.chkAllAdmin').prop('checked', false);
                $('.chkAdmin').prop('checked', false);

                $('.chkAllMaster').prop('checked', false);
                $('.chkMaster').prop('checked', false);

                $('.chkAllFundings').prop('checked', false);
                $('.chkFundings').prop('checked', false);

                $('.chkAllWorkspace').prop('checked', false);
                $('.chkWorkspace').prop('checked', false);

                $('.chkAllReports').prop('checked', false);
                $('.chkReports').prop('checked', false);
            }
        }

    </script>
}


